package tum0r.data.serialize;

import java.lang.reflect.Field;
import java.util.LinkedList;

/**
 * 工程: TestSerialize<br>
 * 包: tum0r.data.serialize<br>
 * 创建者: tum0r<br>
 * 创建时间: 2020/8/22 21:21<br>
 * <br>
 */
public class Reflect {
    public static LinkedList<Field> getFields(Object o) {
        return getFields(o, true);
    }

    public static LinkedList<Field> getFields(Object o, boolean justPublic) {
        LinkedList<Field> result = null;
        if (o != null) {
            result = getFields(o.getClass(), justPublic);
        }
        return result;
    }

    public static LinkedList<Field> getFields(Class<?> c) {
        return getFields(c, true);
    }

    public static LinkedList<Field> getFields(Class<?> c, boolean justPublic) {
        LinkedList<Field> result = new LinkedList<>();
        if (c != null) {
            Field[] fields;
            if (justPublic) {
                fields = c.getFields();
            } else {
                fields = c.getDeclaredFields();
            }
            for (Field field : fields) {
                if (field != null) {
                    field.setAccessible(true);
                    result.add(field);
                }
            }
        }
        return result;
    }

    public static Object getFieldValue(Field field, Object o) {
        Object result = null;
        try {
            result = field.get(o);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return result;
    }
}
